package com.wejoy.video.middle.station.admin.controller.system;

import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.domain.system.SysBaseParam;
import com.wejoy.video.middle.station.data.service.system.ISysBaseParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

/**
 * 系统--基础参数
 *
 * @author Administrator
 * @date 2021年10月26日 16:36:52
 */
@RestController
@RequestMapping("/system/sysBaseParam")
public class SysBaseParamController extends BaseController {

    @Autowired
    private ISysBaseParamService sysBaseParamService;

    /**
     * 保存或更新
     *
     * @param sysBaseParamReq sysBaseParamReq
     * @return RespJSON
     */
    @PostMapping("/save")
    @RoleCheck(name = "sys_base_param_add,sys_base_param_edit")
    public RespJSON save(@RequestBody SysBaseParam sysBaseParamReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        sysBaseParamService.save(sysBaseParamReq, loginSysUser.getName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param sysBaseParamReq sysBaseParamReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "sys_base_param_list")
    public RespJSON getPageList(@RequestBody SysBaseParam sysBaseParamReq) {
        int count = sysBaseParamService.getCount(sysBaseParamReq);
        List<SysBaseParam> list = new ArrayList<>();
        if (count > 0) {
            list = sysBaseParamService.getPageList(sysBaseParamReq);
        }
        PageResult result = new PageResult(sysBaseParamReq.getPageNo(), sysBaseParamReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(sysBaseParamService.getOneById(id));
    }

    /**
    * 审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/audit")
    @ResponseBody
    @RoleCheck(name = "sys_base_param_audit")
        public RespJSON audit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysBaseParamService.audit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    @PostMapping("/delete")
    public RespJSON delete(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysBaseParamService.delete(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
    * 取消审核
    *
    * @param id id
    * @return RespJSON
    */
    @PostMapping("/unaudit")
    @RoleCheck(name = "sys_base_param_unAudit")
    @ResponseBody
        public RespJSON unaudit(Long id, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        WJAssert.notNull(id, "ID 不合法");
        sysBaseParamService.unaudit(id, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 通过code查value
     *
     * @param unicode
     * @return
     */
    @PostMapping("/selectValueByUnicode")
    public RespJSON selectValueByUnicode(String unicode) {
        WJAssert.isMaxLength(unicode, 33, "参数非法");
        String value = sysBaseParamService.getOneByUniCode(unicode).getValue();
        return RespJSON.success(value);
    }

    /**
     * 通过code设置value
     *
     * @param sysBaseParamReq
     * @return
     */
    @PostMapping("/setValueByUnicode")
    public RespJSON setValueByUnicode(@RequestBody SysBaseParam sysBaseParamReq) {
        WJAssert.notNull(sysBaseParamReq.getUniCode(), "参数非法");
        WJAssert.notNull(sysBaseParamReq.getValue(), "参数值非法");
        SysBaseParam sysBaseParamDB = sysBaseParamService.getOneByUniCode(sysBaseParamReq.getUniCode());
        sysBaseParamDB.setValue(sysBaseParamReq.getValue());
        return RespJSON.success(sysBaseParamService.updateSelectiveById(sysBaseParamDB));
    }
}
